#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>

int main(){
	int fd[2];
	char buf[1025];
	int ret = pipe(fd), n;
	if (ret == -1) {
		perror("pipe error");
		return 0;
	}

	memset(buf, 0, 1025);
	pid_t pid = fork();

	if (pid == -1) {
		perror("fork error");
		return 0;
	} else if (pid == 0){
		close(fd[1]);
		n = read(fd[0], buf, 1024);
		if (-1 == n) {
			perror("read error");
			close(fd[0]);
			return 0;
		}
		printf("read %d byte data: %s\n", n, buf);
		close(fd[0]);
	} else {
		close(fd[0]);
		strcpy(buf, "HelloWorld");
		n = write(fd[1], buf, strlen(buf));
		if (n == -1) {
			perror("write error");
			close(fd[1]);
			return 0;
		}
		wait(0);
		close(fd[1]);
	}
	return 0;
}
